package com.df.my_net_game.net.base;

import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.function.Supplier;

@Slf4j
public abstract class TcpServerCB<T> implements IServerCB<T>{


    @Override
    public void onChannelInit(ChannelPipeline pipeline, GTcpServer server) {
        List<Supplier<ChannelHandler>> handlers = server.getHandlers();
        if(handlers!=null && !handlers.isEmpty()){
            for (Supplier<ChannelHandler> supplier : handlers) {
                pipeline.addLast(supplier.get());
            }
        }
    }

    @Override
    public void onConnected(ChannelHandlerContext ctx,GTcpServer server) {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String hostAddress = socketAddress.getAddress().getHostAddress();
        Integer port = socketAddress.getPort();
        log.info("Channel Active:  "+hostAddress+":"+port);
    }

    @Override
    public void onDisconnected(ChannelHandlerContext ctx,GTcpServer server) {
        //断开连接时
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String hostAddress = socketAddress.getAddress().getHostAddress();
        Integer port = socketAddress.getPort();
        log.info("Channel InActive:  "+hostAddress+":"+port);
    }

}
